package nuaa.service;

import nuaa.dao.IIndexDao;
import nuaa.domain.Group;
import nuaa.util.UtilSqlSession;
import nuaa.util.UtilString;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * IndexController层对应的服务类
 */
public class IndexService {
    /**
     * 搜索栏查找出游队伍
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    public List<Group> search(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1.获取提交内容
        String sgroup = request.getParameter("search-group");
        String sdest = request.getParameter("search-destination");
        List<Group> groups;
        //2.得到dao层代理对象
        IIndexDao indexDao = UtilSqlSession.getSqlSession().getMapper(IIndexDao.class);
        //3.判别是否为空
        if (!UtilString.isEmpty(sgroup)){
            /*根据输入的队伍名进行模糊查询*/
            //1.调用dao层的方法，传入sgroup，获取结果
            groups=indexDao.searchByGroup(sgroup);
        }else {
            /*根据出游地进行模糊查询*/
            //1.调用dao层的方法，传入sdest，获取结果
            groups=indexDao.searchByDestination(sdest);
        }
        //4.返回查询队伍列表
        return groups;
    }

    /**
     * 获取所有的队伍
     * @return
     * @throws IOException
     */
    public List<Group> getGroups() throws IOException {
        /*从dao层获取到所有队伍*/
        //1.获取dao对象
        SqlSession sqlSession = UtilSqlSession.getSqlSession();
        IIndexDao iIndexDao = sqlSession.getMapper(IIndexDao.class);
        //2.调用对象的getGroups()方法得到列表
        List<Group> groups = iIndexDao.getGroups();
        for (Group group : groups) {
            System.out.println(group);
        }
        //3.返回列表对象
        return groups;
    }

    /**
     * 确定跳转的映射方法体
     * @param pageNum
     * @param item
     * @return
     */
    public String personalInformation(String pageNum, String item) {
        String nextPage="/user/";
        switch (pageNum){
            case "2":
                nextPage+="personalScore";
                break;
            case "3":
                nextPage+="personalEvaluation";
                break;
            case "4":
                nextPage+="personalHistory";
                break;
            case "5":
                nextPage+="personalCollection";
                break;
            case "1":
            default:
                nextPage+="personalInfo";
                break;
        }
        return nextPage;
    }

    /**
     * 确定跳转的映射方法体
     * @param pageNum
     * @param item
     * @return
     */
    public String adminInfo(String pageNum, String item) {
        String nextPage="/admin/";
        switch (pageNum){
            default:
            case "1":
                nextPage+="announce";
                break;
            case "2":
                nextPage+="deleteGroup";
                break;
        }
        return nextPage;
    }
}
